package com.hz.userservice.mapper;

import com.hz.userservice.model.entity.UserEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Select("select * from users where id = #{id}")
    public UserEntity findById(@Param("id") Integer id);

    @Select("select * from users where id = #{id} and user_type = 3")
    public UserEntity findStudentById(@Param("id") Integer id);

    @Select("select * from users where id = #{id} and user_type = 2")
    public UserEntity findTeacherById(@Param("id") Integer id);

    @Select("select * from users where user_type = 2")
    public List<UserEntity> getAllTeachers();

    @Select("select * from users where username = #{username}")
    public UserEntity findByUsername(@Param("username") String username);
    @Select("select * from users where username = #{username} and user_type = #{userType}")
    public UserEntity findByUsernameAndUsertype(@Param("username") String username,@Param("userType") Integer userType);

    @Select("select * from users")
    public List<UserEntity> allUser();

    @Insert("insert into users values (null,#{username},#{realname},#{student_id},#{pwd_salt},#{pwd},#{pwd_valid_date},#{user_type},#{pwd_error_count},#{class_id},#{grade_id})")
    public int addUser(UserEntity userEntity);

    @Delete("delete from users where id=#{id}")
    public int deleteUserById(@Param("id") int id);

    @Update("update users set pwd = #{pwd},pwd_valid_date = #{validDate} where id = #{userId}")
    public int updatePwd(@Param("userId") Integer userId,@Param("pwd") String pwd,@Param("validDate") long validDate);

    @Update("update users set pwd_error_count = #{errorCount} where id = #{userId}")
    public int updatePwdErrorCount(@Param("id") int userId,@Param("pwd_error_count") int errorCount);

}
